<template>
  <div class="member-order">
    <XtxTabs v-model="activeName" @tab-click="changeTab"
      ><XtxTabsPanal
        v-for="i in apiOrderStatus"
        :key="i"
        :label="i.label"
        :name="i.name"
      >
        <div class="order-list">
          <div v-if="loading" class="loading"></div>
          <div class="none" v-if="!loading && orderList.length === 0">
            暂无数据
          </div>
          <!-- 商品列表 -->
          <OrderItem
            @on-cancel="handelCancel"
            v-for="item in orderList"
            :key="item.id"
            :order="item"
            @on-delete="deleteOrderList"
            @on-confirm="handelConfirm"
            @on-logistics="handelLogistics"
          ></OrderItem>
        </div>
      </XtxTabsPanal>
    </XtxTabs>
  </div>
  <!-- 取消原因 -->
  <OrderCancel ref="orderCancelCom" />
  <!-- 查看物流组件 -->
  <OrderLogistics ref="orderLogistics"></OrderLogistics>
</template>

<script>
import { reactive, ref, watch } from 'vue'
import { orderStatus } from '@/api/constant.js'
import OrderItem from './componerts/order-item.vue'
import { findOrderList, deleteOrder, cofirmOrder } from '@/api/order.js'
import OrderCancel from './componerts/order-cancel.vue'
import Confirm from '../../../../../erabbit-client-pc-124/src/components/library/Confirm'
import Message from '@/components/library/Message'
import OrderLogistics from './componerts/order-logistics.vue'
export default {
  name: 'MemberOrder',
  components: {
    OrderItem,
    OrderCancel,
    OrderLogistics
  },
  setup() {
    const apiOrderStatus = ref(null)
    apiOrderStatus.value = orderStatus
    // 默认激活
    const activeName = ref(apiOrderStatus.value[0].name)
    // 获取订单列表参数
    const reqParams = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0
    })
    // 订单列表
    const orderList = ref([])
    const loading = ref(true)
    // 获取订单列表
    const getOrderList = () => {
      loading.value = true
      findOrderList(reqParams).then((data) => {
        orderList.value = data.result.items
        loading.value = false
      })
    }
    // 获取订单数据
    watch(
      reqParams,
      () => {
        getOrderList()
      },
      { immediate: true }
    )
    const changeTab = ({ name, index }) => {
      reqParams.page = 1
      reqParams.orderState = index
    }
    // 组件实例
    const orderCancelCom = ref(null)
    // 点击取消
    const handelCancel = (order) => {
      orderCancelCom.value.open(order)
    }
    // 删除订单
    const deleteOrderList = (order) => {
      Confirm({ title: '温馨提示', text: '亲，您确认删除该订单吗？' })
        .then(() => {
          deleteOrder(order.id).then(() => {
            Message('success', '删除成功')
            getOrderList()
          })
        })
        .catch(() => {})
    }
    return {
      activeName,
      apiOrderStatus,
      orderList,
      changeTab,
      loading,
      orderCancelCom,
      handelCancel,
      deleteOrderList,
      ...useConfirm(),
      ...useLogistics()
    }
  }
}
// 确认收货
export const useConfirm = () => {
  const handelConfirm = (order) => {
    Confirm({
      title: '温馨提示',
      text: '亲，您确认收货吗?确认后货款将打给对方'
    })
      .then(() => {
        cofirmOrder(order.id).then(() => {
          Message('success', '确认收货成功')
          // 待收货--》待评价
          order.orderState = 4
        })
      })
      .catch(() => {})
  }
  return { handelConfirm }
}

// 产看物流逻辑
export const useLogistics = () => {
  const orderLogistics = ref(null)
  const handelLogistics = (order) => {
    orderLogistics.value.open(order)
  }
  return { handelLogistics, orderLogistics }
}
// 取消订单逻辑
export const useCancel = () => {
  // 组件实例
  const orderCancelCom = ref(null)
  // 点击取消
  const handelCancel = (order) => {
    console.log(orderCancelCom.value)
    orderCancelCom.value.open(order)
  }
  return { handelCancel, orderCancelCom }
}
</script>

<style lang="less" scoped>
.member-order {
  height: 100%;
}
.order-list {
  background: #fff;
  padding: 20px;
}
.order-list {
  padding: 20px;
  position: relative;
  min-height: 400px;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, 0.9) url(../../../assets/images/loading.gif)
    no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
